/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ad_NhanVienInternalFrame.java
 *
 * Created on 04-04-2012, 22:39:01
 */
package GUI;

import DAO.MySQLDataAccessHelper;
import DAO.NhanVienDAO;
import POJO.NhanVienPoJo;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tran quoc thinh
 */
public class Ad_NhanVienInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form Ad_NhanVienInternalFrame */
    public Ad_NhanVienInternalFrame() {
        initComponents();
    }
    private int a=-1;
    private void LayNhanVien(){
        try{         
           ResultSet rs=NhanVienDAO.LayBangNhanVien();
           DefaultTableModel model =new DefaultTableModel();
           model.addColumn("MaNhanVien");
           model.addColumn("TenNhanVien");
           model.addColumn("DienThoai");
           model.addColumn("DiaChi");
           model.addColumn("NgaySinh");
           model.addColumn("QuyenHan"); 
           model.addColumn("UserName");  
           model.addColumn("PassWord");  
           while(rs.next()){
               Object[] a={rs.getString("MaNV"),rs.getString("TenNV"),rs.getString("DienThoai"),
               rs.getString("DiaChi"),rs.getString("NgaySinh"),rs.getString("QuyenHan"),rs.getString("UserName"),rs.getString("PassWord")};
               model.addRow(a);
           }
           this.jTable1.setModel(model);        
        }catch(Exception ex){
            System.err.println(ex);
        }       
        MySQLDataAccessHelper helper=new MySQLDataAccessHelper();
        helper.Close();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txt_TenNhanVien = new javax.swing.JTextField();
        txt_DienThoai = new javax.swing.JTextField();
        txt_NgaySinh = new javax.swing.JTextField();
        txt_DiaChi = new javax.swing.JTextField();
        txt_TenDangNhap = new javax.swing.JTextField();
        txt_QuyenHan = new javax.swing.JTextField();
        btn_CapNhat = new javax.swing.JButton();
        btn_Them = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        txt_MaNhanVien = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txt_MatKhau = new javax.swing.JTextField();
        btn_BoQua = new javax.swing.JButton();
        btn_Xoa = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Quản lý nhân viên");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setText("Tên Nhân Viên:");

        jLabel2.setText("Địa Chỉ:");

        jLabel3.setText("Ngày Sinh(dd-MM-yyyy):");

        jLabel4.setText("Điện Thoại:");

        jLabel5.setText("Quyền Hạn:");

        jLabel6.setText("Tên Đăng Nhập:");

        txt_NgaySinh.setText("dd-MM-yyyy");

        btn_CapNhat.setText("Cập Nhật");
        btn_CapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CapNhatActionPerformed(evt);
            }
        });

        btn_Them.setText("Thêm");
        btn_Them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ThemActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel7.setText("Mã Nhân Viên:");

        jLabel8.setText("Mật Khẩu:");

        btn_BoQua.setText("Bỏ Qua");
        btn_BoQua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_BoQuaActionPerformed(evt);
            }
        });

        btn_Xoa.setText("Xóa");
        btn_Xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_XoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txt_TenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txt_MaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(105, 105, 105)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel8)))
                                    .addComponent(jLabel3))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txt_NgaySinh, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                                    .addComponent(txt_TenDangNhap, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txt_QuyenHan, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                    .addComponent(txt_MatKhau, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
                                .addGap(30, 30, 30))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txt_DienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txt_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(484, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btn_Them, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btn_CapNhat)
                                .addGap(18, 18, 18)
                                .addComponent(btn_Xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(btn_BoQua, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 648, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(63, 63, 63))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_MaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txt_TenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txt_DienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(txt_NgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(txt_QuyenHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txt_TenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_MatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Them)
                    .addComponent(btn_Xoa)
                    .addComponent(btn_CapNhat)
                    .addComponent(btn_BoQua))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
// TODO add your handling code here:
    this.LayNhanVien();
}//GEN-LAST:event_formInternalFrameOpened

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
// TODO add your handling code here:
    a=this.jTable1.getSelectedRow();    
    this.txt_MaNhanVien.setText(this.jTable1.getValueAt(a, 0).toString());
    this.txt_TenNhanVien.setText(this.jTable1.getValueAt(a, 1).toString());
    this.txt_DienThoai.setText(this.jTable1.getValueAt(a, 2).toString());
    this.txt_DiaChi.setText(this.jTable1.getValueAt(a, 3).toString());
    this.txt_NgaySinh.setText(this.jTable1.getValueAt(a, 4).toString());
    this.txt_QuyenHan.setText(this.jTable1.getValueAt(a, 5).toString());
    this.txt_TenDangNhap.setText(this.jTable1.getValueAt(a, 6).toString());
    this.txt_MatKhau.setText(this.jTable1.getValueAt(a, 7).toString());
}//GEN-LAST:event_jTable1MouseClicked
private void clean(){
    this.txt_DiaChi.setText(null);
    this.txt_DienThoai.setText(null);
    this.txt_MaNhanVien.setText(null);
    this.txt_MatKhau.setText(null);
    this.txt_NgaySinh.setText(null);
    this.txt_QuyenHan.setText(null);
    this.txt_TenDangNhap.setText(null);
    this.txt_TenNhanVien.setText(null);
    
}
private void btn_CapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CapNhatActionPerformed
// TODO add your handling code here:     
    String manv=this.txt_MaNhanVien.getText().trim();
    String diachi=this.txt_DiaChi.getText().trim();    
    String dienthoai=this.txt_DienThoai.getText().trim();
    String matkhau=this.txt_MatKhau.getText().trim();
    String s=this.txt_NgaySinh.getText().trim().toString();
    Date ngaysinh = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");   
        try {
            ngaysinh=sdf.parse(this.txt_NgaySinh.getText().trim().toString());
        } catch (ParseException ex) {
            Logger.getLogger(Ad_NhanVienInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    /*try {
        String nc=this.txt_NgaySinh.getText().trim().toString();
        nc=nc.replace("-", "/");
        ngaysinh= sdf.parse(nc);

    } catch (ParseException ex) {
        Logger.getLogger(Ad_XuatChieuInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
    } */      
    //java.util.Date myDate = new java.util.Date(ngaysinh);
    java.sql.Date sqlDate = new java.sql.Date(ngaysinh.getTime());  

    int quyenhan=Integer.parseInt(this.txt_QuyenHan.getText().trim());
    String username=this.txt_TenDangNhap.getText().trim();
    String tennv=this.txt_TenNhanVien.getText().trim(); 
    
    NhanVienPoJo nv=new NhanVienPoJo();
    nv.setDiaChi(diachi);
    nv.setDienThoai(dienthoai);
    nv.setMaNV(manv);
    nv.setNgaySinh(sqlDate);
    nv.setPassword(matkhau);
    nv.setQuyenHan(quyenhan);
    nv.setTenNV(tennv);
    nv.setUserName(username);
    int n=NhanVienDAO.SuaNhanVien(nv);
    if(n==1){
        JOptionPane.showMessageDialog(null,"Sua Nhan Vien","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
        this.LayNhanVien();
         clean();
    }else{
         JOptionPane.showMessageDialog(null,"Sua Nhan Vien","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_btn_CapNhatActionPerformed

    private void btn_ThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ThemActionPerformed
        // TODO add your handling code here:
        String manv=this.txt_MaNhanVien.getText().trim();
        String diachi=this.txt_DiaChi.getText().trim();    
        String dienthoai=this.txt_DienThoai.getText().trim();
        String matkhau=this.txt_MatKhau.getText().trim();
        String s=this.txt_NgaySinh.getText().trim().toString();
        Date ngaysinh = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");   
        try {
            ngaysinh= sdf.parse(this.txt_NgaySinh.getText().trim().toString());
        } catch (ParseException ex) {
            Logger.getLogger(Ad_NhanVienInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }   
        //java.util.Date myDate = new java.util.Date(ngaysinh);
        //java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());

        int quyenhan=Integer.parseInt(this.txt_QuyenHan.getText().trim());
        String username=this.txt_TenDangNhap.getText().trim();
        String tennv=this.txt_TenNhanVien.getText().trim(); 
        NhanVienPoJo nv=new NhanVienPoJo();
        nv.setDiaChi(diachi);
        nv.setDienThoai(dienthoai);
        nv.setMaNV(manv);
        nv.setNgaySinh(ngaysinh);
        nv.setPassword(matkhau);
        nv.setQuyenHan(quyenhan);
        nv.setTenNV(tennv);
        nv.setUserName(username);
        int n=NhanVienDAO.ThemNhanVien(nv);
        if(n==1){
            JOptionPane.showMessageDialog(null,"Thêm Nhan Vien","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            this.LayNhanVien();
             clean();
        }else{
             JOptionPane.showMessageDialog(null,"Thêm Nhan Vien","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
        }
        LayNhanVien();

    }//GEN-LAST:event_btn_ThemActionPerformed

    private void btn_BoQuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_BoQuaActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btn_BoQuaActionPerformed

    private void btn_XoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_XoaActionPerformed
        // TODO add your handling code here:
         if(a==-1){
              JOptionPane.showMessageDialog(null,"Thông Báo","Chưa Chọn",JOptionPane.INFORMATION_MESSAGE);
         }else{
              String manv=this.jTable1.getValueAt(a, 0).toString();
              int n=NhanVienDAO.XoaNhanVien(manv);
             if (n == 1) {
                 JOptionPane.showMessageDialog(null, "Xóa Nhan Vien", "Thanh Cong", JOptionPane.INFORMATION_MESSAGE);
                 this.LayNhanVien();
             } else {
                 JOptionPane.showMessageDialog(null, "Xóa Nhan Vien", "Khong Thanh Cong", JOptionPane.INFORMATION_MESSAGE);
             }
             LayNhanVien();
             clean();
              
         }
    }//GEN-LAST:event_btn_XoaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_BoQua;
    private javax.swing.JButton btn_CapNhat;
    private javax.swing.JButton btn_Them;
    private javax.swing.JButton btn_Xoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txt_DiaChi;
    private javax.swing.JTextField txt_DienThoai;
    private javax.swing.JTextField txt_MaNhanVien;
    private javax.swing.JTextField txt_MatKhau;
    private javax.swing.JTextField txt_NgaySinh;
    private javax.swing.JTextField txt_QuyenHan;
    private javax.swing.JTextField txt_TenDangNhap;
    private javax.swing.JTextField txt_TenNhanVien;
    // End of variables declaration//GEN-END:variables
}
